//
//  CMBDTabBarController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/7.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBTabBarController: BaseTabBarViewController {
    
    lazy var composeButton : UIButton = UIButton(imageName: "发布-默认", selectedImage: "发布-关闭")
    
    private lazy var imagePickerController : CMDBPickerController = {
        
        let imagePickerController = CMDBPickerController()
        
        imagePickerController.maximumNumberOfSelection = 9
        
        imagePickerController.showEmptyGroups = false
        
        imagePickerController.assetsFilter = ALAssetsFilter.allPhotos()
        
        imagePickerController.showEmptyGroups = false
        
        imagePickerController.delegate        = self
        
        return imagePickerController
        
    }()

    
    private lazy var popoverAnimator : PopoverAnimator = PopoverAnimator {[weak self] (presented) -> () in
        self?.composeButton.selected = presented
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupComposeBtn()
        
    }

}

//MARK: setupUI
extension CMDBTabBarController{

    internal func setupComposeBtn(){
        
        composeButton.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        
        composeButton.addTarget(self, action: #selector(composeButtonClick), forControlEvents: .TouchUpInside)
        
        tabBar.addSubview(composeButton)
        
    }
    
}

extension CMDBTabBarController {
    
    @objc internal func composeButtonClick(){
    
        let popViewController = CMDBHomePopViewController()
        
        popViewController.superVC = self
        
        popViewController.modalPresentationStyle = .Custom
        
        popoverAnimator.isPusblish                = true
        
        popViewController.transitioningDelegate  = popoverAnimator
       
        popoverAnimator.presentedFrame = CGRect(x: KleftMargin, y: 0.55 * CMDB_Screen_Height, width: CMDB_Screen_Width - 2 * KleftMargin, height: 0.35 * CMDB_Screen_Height)
        
        presentViewController(popViewController, animated: true, completion: nil)
    
    }
    
}

extension CMDBTabBarController : UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
    func addImage(){
        
        if !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
            return
        }
        
        self.presentViewController(self.imagePickerController, animated: true,completion: nil)
        
        
    }
}

extension CMDBTabBarController : ZYQAssetPickerControllerDelegate{
    
    func assetPickerController(picker: ZYQAssetPickerController!, didFinishPickingAssets assets: [AnyObject]!) {
        
        var selectedImages = [UIImage]()
        
        for (index,asset) in assets.enumerate() {
            
            let assetA     = asset as! ALAsset
            
            let unMCgImage = assetA.defaultRepresentation().fullScreenImage()
            
            let cgImage    = unMCgImage.takeUnretainedValue()
            
            let image      = UIImage(CGImage: cgImage)
            
            selectedImages.append(image)
        }
        
        let addImageVc     = CMDBPublishPhotosController()
        
        addImageVc.images  = selectedImages
        
        presentedViewController?.dismissViewControllerAnimated(true, completion: {
            self.presentViewController(addImageVc, animated: true,completion: nil)
        })
    
        
    }
    
}

